package com.onehouse.trigger.http;

import com.onehouse.api.dto.DailyRoomPriceSetDTO;
import com.onehouse.api.vo.RoomPriceVO;
import com.onehouse.domain.room.model.RoomPriceEntity;
import com.onehouse.domain.room.service.IRoomPriceService;
import com.onehouse.types.constants.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.stream.Collectors;

@RestController
@CrossOrigin("*")
@RequestMapping("/room-price")
public class RoomPriceController {

    @Autowired
    private IRoomPriceService roomPriceService;
    
    private RoomPriceVO convertToDTO(RoomPriceEntity entity) {
        RoomPriceVO dto = new RoomPriceVO();
        dto.setRoomTypeId(entity.getRoomTypeId());
        dto.setPrice(entity.getPrice());
        dto.setDate(entity.getDate());
        return dto;
    }

    private RoomPriceEntity convertToEntity(RoomPriceVO dto) {
        return RoomPriceEntity.builder()
                .roomTypeId(dto.getRoomTypeId())
                .price(dto.getPrice())
                .date(dto.getDate())
                .build();
    }

    @Operation(summary = "列出所有房间价格")
    @GetMapping("/list")
    public AjaxResult listAll() {
        return AjaxResult.success(
            roomPriceService.listAllRoomPrice().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList())
        );
    }

    @Operation(summary = "按时间范围查询房间价格")
    @GetMapping("/list/range")
    public AjaxResult listByDateRange(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                      @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        try {
            return AjaxResult.success(
                roomPriceService.listRoomPriceByDateRange(startTime, endTime).stream()
                    .map(entity -> {
                        DailyRoomPriceSetDTO dto = new DailyRoomPriceSetDTO();
                        dto.setDate(entity.getDate());
                        dto.setRoomPrices(entity.getRoomPrices().stream()
                            .map(this::convertToDTO)
                            .collect(Collectors.toList()));
                        return dto;
                    })
                    .collect(Collectors.toList())
            );
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "创建单日房间价格")
    @PostMapping("/create")
    public AjaxResult createRoomPrice(@RequestBody RoomPriceVO roomPriceVO) {
        try {
            roomPriceService.createRoomPrice(convertToEntity(roomPriceVO));
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "创建日期范围内的房间价格")
    @PostMapping("/create/range")
    public AjaxResult createRoomPriceByDateRange(@RequestBody RoomPriceVO roomPriceVO,
                                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        try {
            roomPriceService.createRoomPriceByDateRange(convertToEntity(roomPriceVO), startTime, endTime);
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "更新单日房间价格")
    @PutMapping("/update")
    public AjaxResult updateRoomPrice(@RequestBody RoomPriceVO roomPriceVO) {
        try {
            roomPriceService.updateRoomPrice(convertToEntity(roomPriceVO));
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "更新日期范围内的房间价格")
    @PutMapping("/update/range")
    public AjaxResult updateRoomPriceByDateRange(@RequestBody RoomPriceVO roomPriceVO,
                                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        try {
            roomPriceService.updateRoomPriceByDateRange(convertToEntity(roomPriceVO), startTime, endTime);
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "删除房间价格")
    @DeleteMapping("/delete/{priceId}")
    public AjaxResult deleteRoomPrice(@PathVariable Long priceId) {
        try {
            roomPriceService.deleteRoomPrice(priceId);
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
